package com.wwt.roll.controller;

import com.alibaba.fastjson.JSON;
import com.wwt.roll.utils.OkHttpUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RollController {
    @GetMapping("/roll")
    public Map rollAccount(String mobilePhone,String securityCode){
        Map r=new HashMap();
        try{
            String log = OkHttpUtils.builder().url("https://passport.pwesports.cn/account/login")
                    .addParam("appId", "2").addParam("mobilePhone", mobilePhone)
                    .addParam("securityCode", securityCode)
                    .post(true) // true 为json提交方式
                    .sync();
            Map logMap= JSON.parseObject(log, HashMap.class);
            if (!logMap.get("code").equals(0))  {
                r.put("code",200);
                r.put("msg","登录失败" );
                return  logMap;
            }
            String token = (String) ((Map) ((Map) ((Map)logMap.get("result")).get("loginResult")).get("accountInfo")).get("token");
            String body= OkHttpUtils.builder().url("https://appactivity.wmpvp.com/app/roll/list")
                    .addParam("pageNum", "1").addParam("pageSize", "20")
                    .addParam("status", "5").addParam("type", "1")
                    .addParam("orderType", "1").addParam("token", token)
                    .get()
                    .sync();
            Map bodyMap= JSON.parseObject(body, HashMap.class);
            Map dataMap= (Map) bodyMap.get("result");
            if (dataMap==null) {
                r.put("code",200);
                r.put("msg","无可参与的roll");
                return  r;
            }
            List<Map> listMap= (List<Map>) dataMap.get("list");
            Integer flat=1;
            while (true){
                flat+=1;
                body= OkHttpUtils.builder().url("https://appactivity.wmpvp.com/app/roll/list")
                        .addParam("pageNum", flat.toString()).addParam("pageSize", "20")
                        .addParam("status", "5").addParam("type", "1")
                        .addParam("orderType", "1").addParam("token", token)
                        .get()
                        .sync();
                bodyMap= JSON.parseObject(body, HashMap.class);
                dataMap= (Map) bodyMap.get("result");
                if (dataMap==null)
                    break;
                listMap.addAll((List<Map>) dataMap.get("list"));
            }
            List<String> msgList=new ArrayList<>();
            msgList.add("Roll有效房间为："+listMap.size()+"个房间");
            Integer isParticipant=0;
            Integer isPassword=0;
            Integer conditionId=0;
            Integer limitNum=0;
            Integer participate=0;
            for (Map map : listMap) {
                //是否已参与
                if (map.get("isParticipant").equals(true)) {
                    isParticipant+=1;
                    continue;
                };
                //是否需要密码
                if (map.get("isPassword").equals(true)) {
                    isPassword+=1;
                    continue;
                }
                Boolean needFollow=false;
                //1为需要关注
                if (map.get("conditionId").equals(1)) needFollow=true;
                //2要关注直播间，3公会成员
                if ((Integer)map.get("conditionId")>1) {
                    conditionId+=1;
                    continue;
                }
                //limitNum人数限制，participantRelationNum参与人数
                if (map.get("limitNum")!=null){
                    if ((map.get("limitNum").equals(map.get("participantRelationNum")))){
                        limitNum+=1;
                        continue;
                    }
                }
                String sync = OkHttpUtils.builder().url("https://appactivity.wmpvp.com/app/roll/join")
                        .addParam("password", "").addParam("rollId", ((Integer) map.get("id")).toString())
                        .addParam("token", token).addParam("needFollow",needFollow.toString())
                        .post(false) // true 为json提交方式
                        .sync();
                participate+=1;
            }
            msgList.add("已参与数量为："+isParticipant);
            msgList.add("需要密码的房间有："+isPassword+"个");
            msgList.add("需要关注直播间或公会内部房："+conditionId+"个");
            msgList.add("参与数量有限的房间："+limitNum+"个");
            msgList.add("本次成功参与："+participate+"个");
            r.put("code",200);
            r.put("msg",msgList);
            return  r;
        }catch (Exception e){
            e.printStackTrace();
            r.put("异常",e);
            return r;
        }
    }

    @GetMapping("/cancel")
    public Map outFollow(String mobilePhone,String securityCode){
        Map r=new HashMap();
        try {
            String log = OkHttpUtils.builder().url("https://passport.pwesports.cn/account/login")
                    .addParam("appId", "2").addParam("mobilePhone", mobilePhone)
                    .addParam("securityCode", securityCode)
                    .post(true) // true 为json提交方式
                    .sync();
            Map logMap= JSON.parseObject(log, HashMap.class);
            if (!logMap.get("code").equals(0))  {
                r.put("code",200);
                r.put("msg","登录失败" );
                return  logMap;
            }
            String token = (String) ((Map) ((Map) ((Map)logMap.get("result")).get("loginResult")).get("accountInfo")).get("token");
            String body= OkHttpUtils.builder().url("https://appengine.wmpvp.com/steamcn/community/follow/getUserFollowList")
                    .addParam("pageNum","1").addParam("pageSize","200")
                    .addParam("token",token)
                    .addParam("id","0").addParam("gameTypeStr","2").addParam("gameType","2")
                    .get()
                    .sync();
            Map bodyMap= JSON.parseObject(body, HashMap.class);
            List<Map> mapList= (List<Map>) ((Map)((Map) bodyMap.get("result")).get("followResponse")).get("followDTOList");

            Integer flat=1;
            while (true){
                flat+=1;
                 body= OkHttpUtils.builder().url("https://appengine.wmpvp.com/steamcn/community/follow/getUserFollowList")
                        .addParam("pageNum",flat.toString()).addParam("pageSize","100")
                        .addParam("token",token)
                        .addParam("id",((Integer) mapList.get(mapList.size()-1).get("id")).toString()).addParam("gameTypeStr","2").addParam("gameType","2")
                        .get()
                        .sync();
                bodyMap= JSON.parseObject(body, HashMap.class);
                List<Map> data=(List<Map>) ((Map)((Map) bodyMap.get("result")).get("followResponse")).get("followDTOList");
                if (data.size()==0){
                    break;
                }
                mapList.addAll(data);
            }
            Integer ht=0;
            Integer ot=0;
            for (Map map : mapList) {
                if ((Integer) map.get("followStatus")==1){
                    String sync = OkHttpUtils.builder().url("https://appengine.wmpvp.com/steamcn/community/follow/doFollow")
                            .addParam("operationType", "false")
                            .addParam("toUserId", ((Integer) map.get("userId")).toString())
                            .addParam("token", token)
                            .post(false) // true 为json提交方式
                            .sync();
                   ot+=1;
                }else {
                    ht+=0;
                }
            }
            r.put("关注人数总量：",mapList.size());
            r.put("互关人数：",ht);
            r.put("本次成功取关人数：",ot);
            return r;
        }catch (Exception e){
            e.printStackTrace();
            r.put("异常",e);
            return r;
        }
    }
}
